<?php

namespace App\Api;

use App\Http\Controllers\Controller;
use App\Models\Department;
use App\Models\Log;
use App\Models\Record;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Http\Request;

class DashboardController extends Controller
{
    public function index(Request $request)
    {

//       一、 系统信息：部门数，员工数，记录数，日志数
        $departmentCount = Department::count();
        $userCount = User::count();
        $recordCount = Record::count();
        $logCount = Log::count();
//      二、业绩排行，按照records排行，同时加载user的name,分为月、周、日，只取前十
        // request中包含了month,week,day,分别代表月、周、日,接受到的数据为数字，比如是月，就从该月的第一天到最后一天
//        其中month的值形式为：2023-05，
        $monthStr = $request->query("month");
        $startMonthTime = null;
        $endMonthTime = null;
        if ($monthStr) {
            //拆分字符串:使用Carbon转换为月,比如$month的值是2023-05，就从5月1日至5月31日，获取$month的所在月份的开始时间的结束时间
            $year = explode("-", $monthStr)[0];
            $month = explode("-", $monthStr)[1];
            $startMonthTime = Carbon::createFromDate($year, $month, 1)->startOfMonth();
            $endMonthTime = Carbon::createFromDate($year, $month, 1)->endOfMonth();
        } else {
            $startMonthTime = Carbon::createFromDate(now()->year, now()->month, 1)->startOfMonth();
            $endMonthTime = Carbon::createFromDate(now()->year, now()->month, 1)->endOfMonth();
        }

        $monthRecords = Record::whereBetween("start_at", [$startMonthTime, $endMonthTime])->with("user")->get();
//        还需要对count进行降序排列

        $monthRecords = $monthRecords->groupBy("user.name")->map(function ($item) {
            return $item->count();
        })->sortDesc()->take(10);
//        最后转换为[[name=>'',count=>''],[name=>'',count=>'']]二维数组的形式
        $resultMonthRecords = collect($monthRecords)->map(function ($item, $key) {
            return [
                "name" => $key,
                "count" => $item
            ];
        })->values();

        //使用Carbon转换为周,比如$week为一个日期，2023-12-11，转换为Carbon对象，就从该周的周一到周日
        $weekStr = $request->query("week");
        $startWeekTime = null;
        $endWeekTime = null;
        if ($weekStr) {
//          $weekStr为  2023-12-11，使用carbon转换为周
//           $weekStr转换为Carbon对象，就从该周的周一到周日
            $year = explode("-", $weekStr)[0];
            $week = Carbon::parse($weekStr)->week;
            $startWeekTime = Carbon::createFromDate($year, 1, 1)->addWeeks($week)->startOfWeek();
            $endWeekTime = Carbon::createFromDate($year, 1, 1)->addWeeks($week)->endOfWeek();
        } else {
            $startWeekTime = Carbon::createFromDate(now()->year, 1, 1)->addWeeks(now()->week)->startOfWeek();
            $endWeekTime = Carbon::createFromDate(now()->year, 1, 1)->addWeeks(now()->week)->endOfWeek();
        }
        $weekRecords = Record::whereBetween("start_at", [$startWeekTime, $endWeekTime])->with("user")->get();
        $weekRecords = $weekRecords->groupBy("user.name")->map(function ($item) {
            return $item->count();
        })->sortDesc()->take(10);
//        最后转换为[name=>'',count=>'']的形式
        $resultWeekRecords = collect($weekRecords)->map(function ($item, $key) {
            return [
                "name" => $key,
                "count" => $item
            ];
        })->values();


//        使用Carbon转换为日,比如$day是一个日期字符串，转换为Carbon对象，就从该日00：00：00到23：59：59
        $day = $request->query("day",now());

        $startDayTime = Carbon::parse($day)->startOfDay();
        $endDayTime = Carbon::parse($day)->endOfDay();
        $dayRecords = Record::whereBetween("start_at", [$startDayTime, $endDayTime])->with("user")->get();
        $dayRecords = $dayRecords->groupBy("user.name")->map(function ($item) {
            return $item->count();
        })->sortDesc()->take(10);
//        最后转换为[name=>'',count=>'']的形式
        $resultDayRecords = collect($dayRecords)->map(function ($item, $key) {
            return [
                "name" => $key,
                "count" => $item
            ];
        })->values();
        return [
            "departmentCount" => $departmentCount,
            "userCount" => $userCount,
            "recordCount" => $recordCount,
            "logCount" => $logCount,
            "monthRecords" => $resultMonthRecords,
            "weekRecords" => $resultWeekRecords,
            "dayRecords" => $resultDayRecords,
        ];
    }
}
